<?php
class editAction extends sfActions
{
	public function execute($request)
	{
		$this->name = $request->getParameter('information_name');
		if ($this->name) {
			$this->information = Doctrine_Core::getTable('DtInformation')->find($this->name);
			if ($this->information)
				$this->information_form = new AdminInformationForm($this->information);
		}
		
		if ($request->hasParameter('information')) {
			$form = $request->getParameter('information');
			$this->status = $this->updateInformationFromRequest($this->information, $form);
			if ($this->status) {
				$this->information->save();
				$this->forward('informations', 'index');
			} else
				return SfView::SUCCESS;
		} else
			return SfView::SUCCESS;
		
	}
	
	
	private function updateInformationFromRequest(&$information, $form)
	{
		if ($form['name']) {
			$c = Doctrine_Core::getTable('DtInformation')->findOneByName($form['name']);
			if (!$c || ($c->name == $information->name)) {
				$information->name = $form['name'];
			} else {
				return false;
			}
		}
		
		if ($form['value'])
			$information->value = $form['value'];

		$information->updated_at = date(DATE_ISO8601);
		return true;
	}
}
?>